package hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.navigator;

import hu.bme.mit.inf.eclipse.v37zen.awesome.Awesome;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.edit.parts.AwesomeEditPart;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.edit.parts.BottomBoxEditPart;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.edit.parts.Desktop2EditPart;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.edit.parts.DesktopEditPart;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.edit.parts.LeftBoxEditPart;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.edit.parts.MenuEditPart;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.edit.parts.MenuNameEditPart;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.edit.parts.MenuitemEditPart;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.edit.parts.MenuitemLabelEditPart;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.edit.parts.MenuitemSubmenuEditPart;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.edit.parts.RightBoxEditPart;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.edit.parts.TopBoxEditPart;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.part.AwesomeDiagramEditorPlugin;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.part.AwesomeVisualIDRegistry;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.providers.AwesomeElementTypes;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.providers.AwesomeParserProvider;

import org.eclipse.gmf.runtime.common.ui.services.parser.IParser;
import org.eclipse.gmf.runtime.common.ui.services.parser.ParserOptions;
import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ITreePathLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.ViewerLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.navigator.ICommonContentExtensionSite;
import org.eclipse.ui.navigator.ICommonLabelProvider;

/**
 * @generated
 */
public class AwesomeNavigatorLabelProvider extends LabelProvider implements
		ICommonLabelProvider, ITreePathLabelProvider {

	/**
	 * @generated
	 */
	static {
		AwesomeDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?UnknownElement", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
		AwesomeDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?ImageNotFound", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	public void updateLabel(ViewerLabel label, TreePath elementPath) {
		Object element = elementPath.getLastSegment();
		if (element instanceof AwesomeNavigatorItem
				&& !isOwnView(((AwesomeNavigatorItem) element).getView())) {
			return;
		}
		label.setText(getText(element));
		label.setImage(getImage(element));
	}

	/**
	 * @generated
	 */
	public Image getImage(Object element) {
		if (element instanceof AwesomeNavigatorGroup) {
			AwesomeNavigatorGroup group = (AwesomeNavigatorGroup) element;
			return AwesomeDiagramEditorPlugin.getInstance().getBundledImage(
					group.getIcon());
		}

		if (element instanceof AwesomeNavigatorItem) {
			AwesomeNavigatorItem navigatorItem = (AwesomeNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return super.getImage(element);
			}
			return getImage(navigatorItem.getView());
		}

		return super.getImage(element);
	}

	/**
	 * @generated
	 */
	public Image getImage(View view) {
		switch (AwesomeVisualIDRegistry.getVisualID(view)) {
		case RightBoxEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://awesome.v37zen.eclipse.inf.mit.bme.hu/1.0?RightBox", AwesomeElementTypes.RightBox_3006); //$NON-NLS-1$
		case LeftBoxEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://awesome.v37zen.eclipse.inf.mit.bme.hu/1.0?LeftBox", AwesomeElementTypes.LeftBox_3005); //$NON-NLS-1$
		case DesktopEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://awesome.v37zen.eclipse.inf.mit.bme.hu/1.0?Desktop", AwesomeElementTypes.Desktop_2001); //$NON-NLS-1$
		case BottomBoxEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://awesome.v37zen.eclipse.inf.mit.bme.hu/1.0?BottomBox", AwesomeElementTypes.BottomBox_3004); //$NON-NLS-1$
		case AwesomeEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Diagram?http://awesome.v37zen.eclipse.inf.mit.bme.hu/1.0?Awesome", AwesomeElementTypes.Awesome_1000); //$NON-NLS-1$
		case MenuitemEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://awesome.v37zen.eclipse.inf.mit.bme.hu/1.0?Menuitem", AwesomeElementTypes.Menuitem_3002); //$NON-NLS-1$
		case MenuEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://awesome.v37zen.eclipse.inf.mit.bme.hu/1.0?Menu", AwesomeElementTypes.Menu_3001); //$NON-NLS-1$
		case TopBoxEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://awesome.v37zen.eclipse.inf.mit.bme.hu/1.0?TopBox", AwesomeElementTypes.TopBox_3003); //$NON-NLS-1$
		case MenuitemSubmenuEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://awesome.v37zen.eclipse.inf.mit.bme.hu/1.0?Menuitem?submenu", AwesomeElementTypes.MenuitemSubmenu_4002); //$NON-NLS-1$
		}
		return getImage("Navigator?UnknownElement", null); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private Image getImage(String key, IElementType elementType) {
		ImageRegistry imageRegistry = AwesomeDiagramEditorPlugin.getInstance()
				.getImageRegistry();
		Image image = imageRegistry.get(key);
		if (image == null && elementType != null
				&& AwesomeElementTypes.isKnownElementType(elementType)) {
			image = AwesomeElementTypes.getImage(elementType);
			imageRegistry.put(key, image);
		}

		if (image == null) {
			image = imageRegistry.get("Navigator?ImageNotFound"); //$NON-NLS-1$
			imageRegistry.put(key, image);
		}
		return image;
	}

	/**
	 * @generated
	 */
	public String getText(Object element) {
		if (element instanceof AwesomeNavigatorGroup) {
			AwesomeNavigatorGroup group = (AwesomeNavigatorGroup) element;
			return group.getGroupName();
		}

		if (element instanceof AwesomeNavigatorItem) {
			AwesomeNavigatorItem navigatorItem = (AwesomeNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return null;
			}
			return getText(navigatorItem.getView());
		}

		return super.getText(element);
	}

	/**
	 * @generated
	 */
	public String getText(View view) {
		if (view.getElement() != null && view.getElement().eIsProxy()) {
			return getUnresolvedDomainElementProxyText(view);
		}
		switch (AwesomeVisualIDRegistry.getVisualID(view)) {
		case RightBoxEditPart.VISUAL_ID:
			return getRightBox_3006Text(view);
		case LeftBoxEditPart.VISUAL_ID:
			return getLeftBox_3005Text(view);
		case DesktopEditPart.VISUAL_ID:
			return getDesktop_2001Text(view);
		case BottomBoxEditPart.VISUAL_ID:
			return getBottomBox_3004Text(view);
		case AwesomeEditPart.VISUAL_ID:
			return getAwesome_1000Text(view);
		case MenuitemEditPart.VISUAL_ID:
			return getMenuitem_3002Text(view);
		case MenuEditPart.VISUAL_ID:
			return getMenu_3001Text(view);
		case TopBoxEditPart.VISUAL_ID:
			return getTopBox_3003Text(view);
		case MenuitemSubmenuEditPart.VISUAL_ID:
			return getMenuitemSubmenu_4002Text(view);
		}
		return getUnknownElementText(view);
	}

	/**
	 * @generated
	 */
	private String getRightBox_3006Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getLeftBox_3005Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getDesktop_2001Text(View view) {
		IParser parser = AwesomeParserProvider.getParser(
				AwesomeElementTypes.Desktop_2001,
				view.getElement() != null ? view.getElement() : view,
				AwesomeVisualIDRegistry.getType(Desktop2EditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			AwesomeDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5003); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getBottomBox_3004Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getAwesome_1000Text(View view) {
		Awesome domainModelElement = (Awesome) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getModkey();
		} else {
			AwesomeDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 1000); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getMenuitem_3002Text(View view) {
		IParser parser = AwesomeParserProvider.getParser(
				AwesomeElementTypes.Menuitem_3002,
				view.getElement() != null ? view.getElement() : view,
				AwesomeVisualIDRegistry
						.getType(MenuitemLabelEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			AwesomeDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5001); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getMenu_3001Text(View view) {
		IParser parser = AwesomeParserProvider.getParser(
				AwesomeElementTypes.Menu_3001,
				view.getElement() != null ? view.getElement() : view,
				AwesomeVisualIDRegistry.getType(MenuNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			AwesomeDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5002); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getTopBox_3003Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getMenuitemSubmenu_4002Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getUnknownElementText(View view) {
		return "<UnknownElement Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	private String getUnresolvedDomainElementProxyText(View view) {
		return "<Unresolved domain element Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	public void init(ICommonContentExtensionSite aConfig) {
	}

	/**
	 * @generated
	 */
	public void restoreState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public void saveState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public String getDescription(Object anElement) {
		return null;
	}

	/**
	 * @generated
	 */
	private boolean isOwnView(View view) {
		return AwesomeEditPart.MODEL_ID.equals(AwesomeVisualIDRegistry
				.getModelID(view));
	}

}
